package com.sky.controller.user;

import com.sky.dto.AddressBookAddDTO;
import com.sky.dto.AddressBookSetDefaultDTO;
import com.sky.dto.AddressBookUpdateDTO;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "收货地址接口")
@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 我的收货地址列表
     * */
    @ApiOperation("我的收货地址列表")
    @GetMapping("list")
    public Result<List<AddressBook>> list(){
        List<AddressBook> list = addressBookService.list();
        return Result.success(list);
    }

    /**
     * 新增收货地址
     * */
    @ApiOperation("新增收货地址")
    @PostMapping
    public Result<Void> add(@RequestBody AddressBookAddDTO dto){
        addressBookService.add(dto);
        return Result.success();
    }

    /**
     * 根据id查询收货地址
     * */
    @ApiOperation("根据id查询收货地址")
    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable("id")Long id){
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    /**
     * 根据id修改收货地址
     * */
    @ApiOperation("根据id修改收货地址")
    @PutMapping
    public Result<Void> update(@RequestBody AddressBookUpdateDTO dto){
        addressBookService.update(dto);
        return Result.success();
    }


    /**
     * 根据id设置默认
     * */
    @ApiOperation("根据id设置默认")
    @PutMapping("/default")
    public Result<Void> setDefault(@RequestBody AddressBookSetDefaultDTO dto){
        addressBookService.setDefault(dto);
        return Result.success();
    }

    /**
     * 根据id删除
     * */
    @ApiOperation("根据id删除")
    @DeleteMapping
    public Result<Void> delete(@RequestParam("id") Long id){
        addressBookService.delete(id);
        return Result.success();
    }


}
